package com.alm.web.controller.alm.getwayinfo;

import com.alm.common.annotation.Log;
import com.alm.common.config.RuoYiConfig;
import com.alm.common.core.controller.BaseController;
import com.alm.common.core.domain.AjaxResult;
import com.alm.common.core.domain.model.LoginUser;
import com.alm.common.core.page.TableDataInfo;
import com.alm.common.core.redis.RedisCache;
import com.alm.common.enums.BusinessType;
import com.alm.common.utils.SecurityUtils;
import com.alm.common.utils.file.FileUploadUtils;
import com.alm.common.utils.file.MimeTypeUtils;
import com.alm.handle.core.domain.entity.rq.LableAddAndUpdateRq;
import com.alm.handle.core.domain.entity.rq.LableInfoRq;
import com.alm.handle.core.domain.entity.rq.RefreshImgRq;
import com.alm.handle.core.domain.entity.rs.QueryScheduleRS;
import com.alm.handle.service.AlmLableService;
import com.alm.handle.util.SoketEleKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.math.BigDecimal;

@RestController
@RequestMapping("/alm/dev/getway/lable")
@Valid
public class LableInfoController extends BaseController {
    @Autowired
    private AlmLableService almLableService;
    @Autowired
    private RedisCache redisCache;
    @PostMapping("/labelist")
    public TableDataInfo list(LableInfoRq lableInfoRq) {
        lableInfoRq.setCommpayId(SecurityUtils.getDeptId());
        lableInfoRq.setIsAdmin(SecurityUtils.isAdmin(SecurityUtils.getUserId()));
        return getDataTable(almLableService.queryByLable(lableInfoRq));
    }
    @PostMapping("/insertLable")
    public AjaxResult insertLable(@RequestBody LableAddAndUpdateRq lableAddAndUpdateRq) {
        Integer integer=almLableService.insertLable(lableAddAndUpdateRq);
        if(integer>0){
            return success(1);
        }else{
            return error("添加失败！");
        }
    }
    @GetMapping("/queryById/{id}")
    public AjaxResult queryById(@PathVariable Integer id) {
        return  success(almLableService.queryByLabelByid(id));
    }

    @PutMapping("/updateLable")
    public AjaxResult updateLable(@RequestBody LableAddAndUpdateRq lableAddAndUpdateRq) {
        return success(almLableService.updateLable(lableAddAndUpdateRq));
    }

    @DeleteMapping("/delLable/{id}")
    public AjaxResult delLable(@PathVariable Integer id) {
        return success(almLableService.delLable(id));
    }
    @GetMapping ("/queryImgInfoById/{id}")
    public AjaxResult queryImgInfoById(@PathVariable Integer id) {
        return success(almLableService.queryImgInfoById(id));
    }
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file,@RequestParam("devid")String devid) throws Exception
    {
        if (!file.isEmpty())
        {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            if (almLableService.setLableImg(avatar, devid)>0)
            {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", avatar);
                // 更新缓存用户头像
                return ajax;
            }
        }
        return error("上传图片异常，请联系管理员");
    }
    @PostMapping("/refreshImg")
    public AjaxResult refreshImg(@RequestBody RefreshImgRq refreshImgRq) {
        try {
            boolean bo= almLableService.refreshImg(refreshImgRq);
        }catch (Exception e){
            e.printStackTrace();
            return error(e.getMessage());
        }
        return success();
    }
    @GetMapping("/querySchedule/{gwMac}")
    public AjaxResult querySchedule(@PathVariable String gwMac) {
        try {
            QueryScheduleRS queryScheduleRS=new QueryScheduleRS();
             String schedule_key= SoketEleKey.LABLE_RE_STATUS+gwMac;
             String status_key= SoketEleKey.LABLE_STATUS+gwMac;
             String re_img=SoketEleKey.RE_IMG+gwMac;
             if(redisCache.hasKey(status_key)||!redisCache.hasKey(re_img)){
                 queryScheduleRS.setIsErrer(true);
                 String msg=redisCache.getCacheObject(status_key);
                 queryScheduleRS.setMsg(msg==null?"发送超时":msg);
                 return success(queryScheduleRS);
             }else{
                 queryScheduleRS.setIsErrer(false);
                 BigDecimal bigDecimal=redisCache.getCacheObject(schedule_key);
                 queryScheduleRS.setSchedule(bigDecimal==null?new BigDecimal(1):bigDecimal);
                 return success(queryScheduleRS);
             }
        }catch (Exception e){
            e.printStackTrace();
            return error(e.getMessage());
        }
    }
}
